package com.app.service.impl;

import com.app.model.SkillTrainingModel;
import com.app.util.StringTool;
import com.app.vo.VideoTrainVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.app.mapper.VideoTrainMapper;
import com.app.model.VideoTrainModel;
import com.app.service.VideoTrainService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;


@Service
public class VideoTrainServiceImpl implements VideoTrainService {

    @Autowired
    VideoTrainMapper videoTrainMapper;

    @Override
    public void insert(VideoTrainModel model) {
        videoTrainMapper.insert(model);
    }

    @Override
    public void update(VideoTrainModel model) {
        videoTrainMapper.updateById(model);
    }

    @Override
    public void delete(int id) {
        videoTrainMapper.deleteById(id);
    }


    @Override
    public Page<VideoTrainModel> getListByModel(VideoTrainModel model) {
        QueryWrapper<VideoTrainModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().orderByDesc("id");
        Page<VideoTrainModel> page = new Page<>();
        page.setCurrent(model.getPage());
        page.setSize(model.getLimit());
        Page<VideoTrainModel> VideoTrainModel = videoTrainMapper.selectPage(page, queryWrapper);
        if (VideoTrainModel == null) {
            return null;
        } else {
            return VideoTrainModel;
        }
    }

    @Override
    public int getModelByCount(VideoTrainModel model) {

        QueryWrapper<VideoTrainModel> queryWrapper = new QueryWrapper<>();
        int count = videoTrainMapper.selectCount(queryWrapper);
        return count;

    }

    @Override
    public Page<VideoTrainModel> bakGetListByModel(VideoTrainModel model) {

        QueryWrapper<VideoTrainModel> queryWrapper = new QueryWrapper<>();
        if (StringTool.isNotEmpty(model.getStartTime())) {
            queryWrapper.ge("crt_time", model.getStartTime());
        }
        if (StringTool.isNotEmpty(model.getEndTime())) {
            queryWrapper.le("crt_time", model.getEndTime());
        }
        if (StringTool.isNotEmpty(model.getKeyWord())) {
            queryWrapper.and(wrapper -> wrapper.like("v_name", model.getKeyWord()));
        }
        queryWrapper.orderByDesc("crt_time");
        Page<VideoTrainModel> page = new Page<>();
        page.setCurrent(model.getPage());
        page.setSize(model.getLimit());
        Page<VideoTrainModel> skillTrainingPage = videoTrainMapper.selectPage(page, queryWrapper);
        if (skillTrainingPage == null) {
            return null;
        } else {
            return skillTrainingPage;
        }
    }

    @Override
    public VideoTrainVo videoList(VideoTrainModel model) {
        Page<VideoTrainModel> videoTrainPage = bakGetListByModel(model);
        List<VideoTrainModel> videoTrainList = videoTrainPage.getRecords();
        for (int i = 0; i < videoTrainList.size(); i++) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Timestamp data = Timestamp.valueOf(videoTrainList.get(i).getCrtTime());
            String currentSimpleDateFormat = simpleDateFormat.format(data);
            videoTrainList.get(i).setCrtTime(currentSimpleDateFormat);
        }
        VideoTrainVo vo = new VideoTrainVo();
        vo.setCount((int) videoTrainPage.getTotal());
        vo.setModelList(videoTrainList);
        return vo;
    }

    @Override
    public VideoTrainModel getVideoById(int id) {
        return videoTrainMapper.selectById(id);
    }

    @Override
    public Integer selectCount() {
        return videoTrainMapper.selectCount(null);
    }

    @Override
    public Page<VideoTrainModel> courseList(Integer page1, Integer limit) {
        Page<VideoTrainModel> page = new Page<>();
        page.setCurrent(page1);
        page.setSize(limit);
        Page<VideoTrainModel> skillTrainingPage = videoTrainMapper.selectPage(page, null);
        if (skillTrainingPage == null) {
            return null;
        } else {
            return skillTrainingPage;
        }
    }

}
